package app.temporary;

import app.common.utils.cjt.CJtUtils;
import app.common.utils.cjt.ao.OtherReceiveCreateAo;
import com.alibaba.excel.EasyExcel;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 入库工具类
 * @author feizns
 * @since 2023/08/15
 */
public abstract class StorageUtilityClass {

    /**
     * 读易excel数据放入存储形式
     * @return {@link List}<{@link GRN}>
     */
    public static List<GRN> readYiDataPutInStorageFormExcel() {
        return EasyExcel.read(("C:\\Users\\28293\\Desktop\\入库数据.xlsx"))
                .head(GRN.class)
                .doReadAllSync();
    }

    public static void main(String[] args) {
        List<Error<OtherReceiveCreateAo.Dto>> errors = new ArrayList<>();
        //读取表格数据
        List<GRN> grn = readYiDataPutInStorageFormExcel();
        //转换为dto
        for (GRN n : grn) {
            if ( n.getNumberOf() != null ) {
                OtherReceiveCreateAo.Dto dto = StorageUtilityClass.grnToDto(n);
                try {
                    CJtUtils.otherReceiveCreate(dto);
                } catch (Exception e) {
                    errors.add(new Error<>(dto, e));
                    e.printStackTrace();
                }
            }
        }
        System.out.println("入库单操作完毕：");
        System.out.println("错误列表");
        List<String> codes = errors.stream().map(Error::getDto).map(OtherReceiveCreateAo.Dto::getCode).collect(Collectors.toList());
        System.out.println(codes);
        errors.forEach(System.out::println);
    }

    /**
     * 入库单,dto
     * @param grn 入库单
     * @return {@link OtherReceiveCreateAo.Dto}
     */
    public static OtherReceiveCreateAo.Dto grnToDto(GRN grn) {
        OtherReceiveCreateAo.Dto dto = new OtherReceiveCreateAo.Dto();
        //字符修改时间
        String stringifyModifyTime = grn.stringifyModifyTime();
        String code = StringUtils.hasText(grn.getCode()) ? grn.getCode() : codeGenerator.code(stringifyModifyTime);
        //生产Code
        dto.setCode(code);
        dto.setExternalCode(code);
        dto.setVoucherType(new OtherReceiveCreateAo.Dto.VoucherType("ST1004"));
        dto.setVoucherDate(stringifyModifyTime);
        dto.setBusiType(new OtherReceiveCreateAo.Dto.BusiType("13"));
        dto.setWarehouse(new OtherReceiveCreateAo.Dto.Warehouse(grn.getWarehouseNumber()));
//        dto.set
        dto.setMemo(String.format("来源=[宜搭] 表单=[%s] 单号=[%s] 备注：[%s] 原单号：[%s]", grn.getSourcesOfForm(), dto.getCode(), grn.getNote(), grn.getExternalNumber()));
        OtherReceiveCreateAo.Dto.RDRecordDetails detail = new OtherReceiveCreateAo.Dto.RDRecordDetails();
        detail.setInventory(new OtherReceiveCreateAo.Dto.RDRecordDetails.Inventory(grn.getMerchantCode()));
        detail.setBaseQuantity(grn.getNumberOf());
        detail.setPrice(grn.getCostPrice());
//        detail.setAmount(NumberUtils.fourDecimalPlaces(grn.getCostPrice().multiply(grn.getNumberOf())));
        detail.setAmount(grn.getAmountOf());
        dto.setRDRecordDetailsObj(detail);
        return dto;
    }

    /**
     * 代码生成器
     */
    private static final CodeGenerator codeGenerator = new CodeGenerator("RKY", 3);

}